import 'package:enterprise_v6_app/app/components/custom_refresh.dart';
import 'package:enterprise_v6_app/app/components/dialog/filter_dialog/custom_filter_right_sheet.dart';
import 'package:enterprise_v6_app/app/core/core.dart';
import 'package:enterprise_v6_app/app/core/http/http.dart';
import 'package:enterprise_v6_app/app/core/utils/interface.dart';
import 'package:enterprise_v6_app/app/data/entities/application/filter/filter_bean.dart';
import 'package:enterprise_v6_app/app/data/entities/application/filter/filter_entities_bean.dart';
import 'package:enterprise_v6_app/app/data/entities/double_control/promise_day_record_list_entity.dart';
import 'package:enterprise_v6_app/app/modules/application/double_prevention_page/risk_control_page/risk_control_controller.dart';

///每日承诺
class DailyCommitmentController extends GetxController {
  /// 筛选右侧栏控制Key
  final popRightScaffoldKey = GlobalKey<ScaffoldState>();

  /// 搜索使用key
  final GlobalKey<CustomEasyRefreshListState> globalKey = GlobalKey();

  /// 搜索字符串
  String? searchX = '';

  /// 筛选条件列表
  List<FilterBean> filterList = [];

  /// 状态筛选条件
  String? status = '';

  /// 选择的项目开始时间
  String? projectBeginDate = '';

  /// 选择的项目结束时间
  String? projectEndDate = '';

  @override
  void onInit() {
    super.onInit();
    initData();
  }

  /// 初始化数据
  Future<void> initData() async {
    // 状态筛选条件
    final List<FilterEntitiesBean> statusList = [];
    statusList.add(FilterEntitiesBean(name: '已承诺', value: '1'));
    statusList.add(FilterEntitiesBean(name: '未承诺', value: '0'));
    // 添加时间类型
    final List<FilterEntitiesBean> timeList = [];
    timeList.add(FilterEntitiesBean(name: '开始时间', value: projectBeginDate));
    timeList.add(FilterEntitiesBean(name: '结束时间', value: projectEndDate));

    //筛选控制bean
    final FilterBean filterBeanStatus = FilterBean(title: '状态', filterList: statusList);
    final FilterBean filterBeanTime = FilterBean(title: '承诺时间', filterList: timeList);
    filterList.add(filterBeanStatus);
    filterList.add(filterBeanTime);
  }

  // 获取数据
  Future<List<PromiseDayRecordListEntity>> equipmentInspectionLedgerList(int index, int size, {String? search}) async {
    final res = await HttpUtil().generateGetRequest<List<PromiseDayRecordListEntity>>(
      Interface.promiseDayRecordList,
      queryParameters: {
        'pageNum': index,
        'pageSize': size,
        'promiseName': searchX, //承诺名称
        'promiseStartTime': projectBeginDate, //承诺开始时间
        'promiseEndTime': projectEndDate, //承诺结束时间
        'type': status, //类型 0:已承诺 1:未承诺
      },
    ).execute();
    List<PromiseDayRecordListEntity>? list = [];
    res.onSuccess((result) {
      if (result.data != null) {
        list = result.data;
      }
    });
    return list!;
  }

  /// 筛选
  void screen() {
    final RiskControlController controller = Get.find<RiskControlController>();
    controller.filterWidget.value = _filterWidget();
    controller.riskRightScaffoldKey.currentState?.openEndDrawer();
    // popRightScaffoldKey.currentState?.openEndDrawer();
  }

  /// 筛选确认
  void screenConfirm(List<FilterBean> value) {
    filterList = value;
    status = '';
    projectBeginDate = '';
    projectEndDate = '';
    for (var i in value) {
      if (i.title == '承诺时间') {
        for (var item in i.filterList) {
          switch (item.name) {
            case '开始时间':
              if (item.value != null && item.value != '') {
                projectBeginDate = item.value as String;
              }
              break;
            case '结束时间':
              if (item.value != null && item.value != '') {
                projectEndDate = item.value as String;
              }
              break;
          }
        }
      }
      if (i.chosenValue.isEmpty) {
        continue;
      }
      if (i.title == '状态') {
        status = i.chosenValue[0].value as String;
      }
    }
    update();
    refreshPage();
  }

  /// 搜索
  void refreshPage() {
    globalKey.currentState?.onRefresh();
  }

  Widget _filterWidget() {
    return Container(
      width: AppDimens.sw - 100.w,
      height: AppDimens.sh,
      color: Colors.white,
      child: CustomFilterRightSheet(
        dataList: filterList,
        onConfirm: screenConfirm,
        isRadio: true,
        isShowTime: true,
      ),
    );
  }
}
